package cn.edu.sgu.www.mhxysy.service.xingyin.impl;

import cn.edu.sgu.www.mhxysy.dto.xingyin.XingyinTexiaoDTO;
import cn.edu.sgu.www.mhxysy.entity.xingyin.XingyinTexiao;
import cn.edu.sgu.www.mhxysy.entity.xingyin.XingyinTexiaoCategory;
import cn.edu.sgu.www.mhxysy.entity.xingyin.XingyinTexiaoType;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinTexiaoCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinTexiaoMapper;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinTexiaoTypeMapper;
import cn.edu.sgu.www.mhxysy.service.xingyin.XingyinTexiaoService;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.vo.treegrid.XingyinTexiaoTreeGrid;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class XingyinTexiaoServiceImpl implements XingyinTexiaoService {

    private final XingyinTexiaoMapper xingyinTexiaoMapper;
    private final XingyinTexiaoTypeMapper xingyinTexiaoTypeMapper;
    private final XingyinTexiaoCategoryMapper xingyinTexiaoCategoryMapper;

    @Autowired
    public XingyinTexiaoServiceImpl(
            XingyinTexiaoMapper xingyinTexiaoMapper,
            XingyinTexiaoTypeMapper xingyinTexiaoTypeMapper,
            XingyinTexiaoCategoryMapper xingyinTexiaoCategoryMapper) {
        this.xingyinTexiaoMapper = xingyinTexiaoMapper;
        this.xingyinTexiaoTypeMapper = xingyinTexiaoTypeMapper;
        this.xingyinTexiaoCategoryMapper = xingyinTexiaoCategoryMapper;
    }

    @Override
    public void distribute(XingyinTexiaoDTO texiaoDTO) {
        Integer xingyinId = texiaoDTO.getXingyinId();

        // 1、删除星印类型对应的特效
        QueryWrapper<XingyinTexiao> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("xingyin_id", xingyinId);

        xingyinTexiaoMapper.delete(queryWrapper);

        // 2、重新添加星印类型对应的特效
        List<Integer> texiaoIds = texiaoDTO.getTexiaoIds();

        if (CollectionUtils.isNotEmpty(texiaoIds)) {
            for (Integer texiaoId : texiaoIds) {
                XingyinTexiao xingyinTexiao = new XingyinTexiao();

                xingyinTexiao.setId(null);
                xingyinTexiao.setTexiaoId(texiaoId);
                xingyinTexiao.setXingyinId(xingyinId);

                xingyinTexiaoMapper.insert(xingyinTexiao);
            }
        }
    }

    @Override
    public void delete(Integer texiaoId, Integer xingyinId) {
        QueryWrapper<XingyinTexiao> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("xingyin_id", xingyinId);
        queryWrapper.eq("texiao_id", texiaoId);

        xingyinTexiaoMapper.delete(queryWrapper);
    }

    @Override
    public List<XingyinTexiaoTreeGrid> listTree(Integer xingyinId) {
        // 查询全部一级特效类型
        List<XingyinTexiaoType> texiaoTypes = xingyinTexiaoTypeMapper.selectList(null);
        List<XingyinTexiaoTreeGrid> treeGrids = new ArrayList<>();

        for (int i = 0; i < texiaoTypes.size(); i++) {
            XingyinTexiaoType texiaoType = texiaoTypes.get(i);

            // 根据类型type查询星印特效类型
            QueryWrapper<XingyinTexiaoCategory> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("type", texiaoType.getId());

            List<XingyinTexiaoCategory> texiaoCategories = xingyinTexiaoCategoryMapper.selectList(queryWrapper);

            // 删除不符合条件的数据
            List<Integer> texiaoIds = selectTexiaoIds(xingyinId);

            texiaoCategories.removeIf(new Predicate<XingyinTexiaoCategory>() {
                @Override
                public boolean test(XingyinTexiaoCategory texiaoCategory) {
                    return !texiaoIds.contains(texiaoCategory.getId());
                }
            });

            // 遍历，生成children
            List<XingyinTexiaoTreeGrid> children = new ArrayList<>();

            texiaoCategories.forEach(new Consumer<XingyinTexiaoCategory>() {
                @Override
                public void accept(XingyinTexiaoCategory xingyinTexiaoCategory) {
                    XingyinTexiaoTreeGrid treeGrid = new XingyinTexiaoTreeGrid();

                    treeGrid.setId(xingyinTexiaoCategory.getId());
                    treeGrid.setName(xingyinTexiaoCategory.getName());
                    treeGrid.setNote(xingyinTexiaoCategory.getNote());
                    treeGrid.setImage(xingyinTexiaoCategory.getImage());

                    children.add(treeGrid);
                }
            });

            XingyinTexiaoTreeGrid parent = new XingyinTexiaoTreeGrid();

            parent.setId(2025 + i);
            parent.setState("open");
            parent.setChildren(children);
            parent.setName(texiaoType.getName());

            treeGrids.add(parent);
        }

        return treeGrids;
    }

    @Override
    public List<Integer> selectTexiaoIds(Integer xingyinId) {
        QueryWrapper<XingyinTexiao> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("texiao_id");
        queryWrapper.eq("xingyin_id", xingyinId);

        List<XingyinTexiao> list = xingyinTexiaoMapper.selectList(queryWrapper);

        return list.stream().map(new Function<XingyinTexiao, Integer>() {
            @Override
            public Integer apply(XingyinTexiao xingyinTexiao) {
                return xingyinTexiao.getTexiaoId();
            }
        }).collect(Collectors.toList());
    }

}